package com.neusoft.dibuy.myarray;

public class HeapSort {
    public static void main(String[] args) {
        int[] is ={ 27, 46, 12, 33, 49, 27, 36, 40, 42, 50, 51 };
        buildHeap(is);
    }

    private static void buildHeap(int[] is) {
        int [] heap = new int[is.length];
        int tail = 0 ;
        int index = 0 ;
        heap[tail]  = is[index];
        int  t = tail;
        int p =-1;
        do{
            p =parent(t );
            if( heap[t ] > heap[p] ){
                int temp = heap[t ];
                heap[t ] = heap[p];
                heap[p] = temp;
            }
        }while (p > 0);
        tail++;

        index =1 ;
        heap[tail]  = is[index];
           t = tail;
          p =-1;
        do{
            p =parent(t );
            if( heap[t ] > heap[p] ){
                int temp = heap[t ];
                heap[t ] = heap[p];
                heap[p] = temp;
            }
        }while (p > 0);
        tail++;
    }
    private static int parent(int tail) {
        return (tail-1)/2;
    }
}
